#!/bin/bash

# Copyright 2014 Intel Corporation, All Rights Reserved.

# Licensed under the Apache License, Version 2.0 (the"License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#  http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.


set -e
set -o xtrace

# setup an EXIT trap; restore original ssh_config on script exit (even if there was no original)
function cleanup()
{
    test -f /etc/ssh/ssh_config.vsmsave && mv /etc/ssh/ssh_config.vsmsave /etc/ssh/ssh_config || rm /etc/ssh/ssh_config
}
trap cleanup EXIT

# replace ssh_config with temporary version; replace on exit by calling cleanup as EXIT handler
test -f /etc/ssh/ssh_config && mv /etc/ssh/ssh_config /etc/ssh/ssh_config.vsmsave
cat <<"EOF" > /etc/ssh/ssh_config
UserKnownHostsFile /dev/null
ConnectTimeout 15
StrictHostKeyChecking no
EOF

TOPDIR=$(cd $(dirname "$0") && pwd)
TEMP=`mktemp`; rm -rfv $TEMP >/dev/null; mkdir -p $TEMP;
#---------------------------------------------
# Usage
#---------------------------------------------

function usage() {
    cat << EOF
Usage: vsm-agent

Simple installation:
    Agent Node: vsm-agent -f config_file

Options:
  --help | -h
    Print usage information.
  --remove | --clean
    Remove all the VSM packages and configuration files.
  -f | --config-file
    Configuratoin file path. Generated by vsm-controller.
    Default use /etc/manifest/server.manifest file.
EOF
    exit 0
}

#---------------------------------------------
# Clear installtion
#---------------------------------------------

function remove() {
    yum remove -y openstack-keystone python-keystone python-keystoneclient
    yum remove -y python-vsmclient vsm vsm-dashboard
    rm -rf /etc/vsm
    rm -rf /etc/vsm-dashboard
    rm -rf /usr/share/vsm-dashboard
    rm -rf /etc/httpd/conf.d/vsm-dashboard.conf
    rm -rf /etc/keystone
    rm -rf /etc/init.d/vsm-*
    rm -rf /etc/init.d/*keystone*

    exit 0
}

#---------------------------------------------
# Read Parameters.
#---------------------------------------------

while [ $# -gt 0 ]; do
  case "$1" in
    -h) usage ;;
    --help) usage ;;
    --remove) remove ;;
    --clean) remove ;;
    *) shift ;;
  esac
  shift
done

#---------------------------------------------

source $TOPDIR/tools/lib/services
source $TOPDIR/tools/lib/mysql

for n in `ls $TOPDIR/tools/lib/ | grep -v services | grep -v manifest | grep -v check`; do
    source $TOPDIR/tools/lib/$n
done

#---------------------------------------------
# Setup NTP client
#---------------------------------------------

#---------------------------------------------
# Create USER
#---------------------------------------------

#---------------------------------------------
# Test connection with controller
# Check manifest
#---------------------------------------------

rm -rf /etc/vsm/*
cp -rf /usr/local/bin/tools/etc/vsm/* /etc/vsm/
mv /etc/vsm/vsm.conf.sample /etc/vsm/vsm.conf

/usr/bin/key
server_manifest
sed -i "s/\(^Defaults.*requiretty.*$\)/#\1/" /etc/sudoers
cp -rf $TOPDIR/tools/etc/sudoers.d/vsm  /etc/sudoers.d/

#---------------------------------------------
# Start services.
#---------------------------------------------

service vsm-agent restart
update-rc.d vsm-agent defaults
sed -i '/exit/d' /etc/rc.local
sed -i "/service vsm-agent restart/d" /etc/rc.local
sed -i "/service vsm-physical restart/d" /etc/rc.local
echo "service vsm-agent restart" >> /etc/rc.local
echo "service vsm-physical restart" >> /etc/rc.local
echo "exit 0" >> /etc/rc.local
chmod +x /etc/vsm/prepools/main
echo "..."
sleep 5
service vsm-physical restart
update-rc.d vsm-physical defaults
echo "The End!"

set +o xtrace

